/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.KhaNangCungUng;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kim
 */
public class KhaNangCungUngDao {

	public static ArrayList<KhaNangCungUng> layDanhSach() {
		ArrayList<KhaNangCungUng> ds = new ArrayList<KhaNangCungUng>();
		String str = "SELECT * FROM KhaNangCungUng";
		String sql = str;

		KetNoi cn = null;
		ResultSet rs = null;
		try {
			cn = new KetNoi();
			rs = cn.docBang(sql);
			while (rs.next()) {
				KhaNangCungUng kncu = new KhaNangCungUng();
				kncu.setMaNCC(rs.getInt("MaNCC"));
				kncu.setMaSP(rs.getInt("MaSP"));

				ds.add(kncu);
			}

		} catch (Exception ex) {
			Logger.getLogger(KhaNangCungUngDao.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			cn.dongKetNoi();
		}
		return ds;
	}

	public static boolean them(KhaNangCungUng tmp) {
		boolean kq = false;
		String sql = String.format(
			 "INSERT INTO KhaNangCungUng(MaNCC,MaSP) VALUES ('%d','%d');", tmp.getMaNCC(), tmp.getMaSP());
		KetNoi cn = null;
		int n;
		n = -1;
		try {
			cn = new KetNoi();
			n = cn.thucThi(sql);
		} catch (SQLException ex) {
			Logger.getLogger(KhaNangCungUngDao.class.getName()).log(Level.SEVERE, null, ex);
		}
		if (n == 1) {
			kq = true;
		}
		cn.dongKetNoi();
		return kq;
	}

	public static boolean xoa(KhaNangCungUng tmp) {
		boolean kq = false;
		String sql = String.format("DELETE FROM KhaNangCungUng WHERE MaNCC = %d and MaSP = %d ", tmp.getMaNCC(), tmp.getMaSP());
		KetNoi cn = null;
		int n;
		n = -1;
		try {
			cn = new KetNoi();
			n = cn.thucThi(sql);
		} catch (SQLException ex) {
			Logger.getLogger(KhaNangCungUngDao.class.getName()).log(Level.SEVERE, null, ex);
		}
		if (n == 1) {
			kq = true;
		}
		cn.dongKetNoi();
		return kq;
	}
}
